package com.googlecode.lookie.boot;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.FelixConstants;
import org.apache.felix.main.AutoActivator;
import org.osgi.framework.BundleException;

public class Launcher
{

    private Launcher()
        throws BundleException
    {
        Map<String, Object> configMap = createConfigMap();
        Felix felix = new Felix( configMap );
        felix.start();
    }

    private Map<String, Object> createConfigMap()
    {
        Map<String, Object> configMap = new HashMap<String, Object>();
        configureFromProperties( configMap );
        configureAutoStartBundles( configMap );
        configureAutoActivators( configMap );
        return configMap;
    }

    private void configureFromProperties( Map<String, Object> configMap )
    {
        Properties properties = new Properties();
        try
        {
            properties.load( getClass().getClassLoader().getResourceAsStream( "felix.properties" ) );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
        for ( Object key : properties.keySet() )
        {
            String name = (String) key;
            configMap.put( name, properties.getProperty( name ) );
        }
    }

    private void configureAutoActivators( Map<String, Object> configMap )
    {
        List<AutoActivator> list = new ArrayList<AutoActivator>();
        list.add( new AutoActivator( configMap ) );
        configMap.put( FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list );
        // configMap.put(FelixConstants.FRAGMENT_VALIDATION_PROP, FelixConstants.FRAGMENT_VALIDATION_WARNING_VALUE);
        configMap.put( FelixConstants.FRAMEWORK_BEGINNING_STARTLEVEL, "2" );
    }

    private void configureAutoStartBundles( Map<String, Object> configMap )
    {
        File bundleDir = new File( "./../bundles" );
        if ( bundleDir.exists() && bundleDir.isDirectory() )
        {
            File[] bundles = bundleDir.listFiles( new FileFilter()
            {
                public boolean accept( File pathname )
                {
                    return pathname.isFile() && pathname.getAbsolutePath().toLowerCase().endsWith( ".jar" );
                }
            } );
            if ( bundles != null )
            {
                StringBuilder sb1 = new StringBuilder();
                boolean first1 = true;
                for ( File bundle : bundles )
                {
                    if ( !first1 )
                    {
                        sb1.append( " " );
                    }
                    sb1.append( "\"file:\"" );
                    sb1.append( bundle.getAbsolutePath() );
                    first1 = false;
                }
                if ( !first1 )
                {
                    // there is at least one bundle
                    String key = AutoActivator.AUTO_START_PROP + ".1";
                    String val = sb1.toString();
                    System.out.println( String.format( "Felix configuration property %s = %s", key, val ) );
                    configMap.put( key, val );
                }
            }
        }
    }

    public static void main( String[] args )
    {
        System.out.println( "Launching" );
        try
        {
            new Launcher();
            System.out.println( "Launched" );
        }
        catch ( BundleException e )
        {
            e.printStackTrace();
            System.out.println( "error while launching" );
        }
    }

}
